<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

require('./spider') // echarts theme
import {debounce} from '@/utils'
import china from './map/json/china.json'
import geoCoordMap from './map/json/geo-coord-map.json'
const animationDuration = 6000

let convertData = function (data) {
  let res = [];
  for (let i = 0; i < data.length; i++) {

    let geoCoord = geoCoordMap[data[i].name];
    if (geoCoord) {
      res.push({
        name: data[i].name,
        value: geoCoord.concat(data[i].value)
      });
    }
  }
  return res;
};

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },

    data: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      chart: null,
      option: {
        title: {
          padding: 10,
        },

        geo: {
          map: 'china',
          // 把地图放大1.2倍
          zoom: 1.2,
          roam: true,
          label: {
            // 鼠标移动显示区域名称
            emphasis: {
              show: true,
              color: '#fff'
            }
          },
          // 地图样式修改
          itemStyle: {
            normal: {
              areaColor: 'rgba(34, 70, 168, 0.7)',
              borderColor: '#0692a4'
            },
            emphasis: {
              areaColor: 'rgba(119, 139, 224, 0.548)'
            }
          }
        },
        series : [
          {
            name: '舆情数量',
            type: 'scatter',
            coordinateSystem: 'geo',
            data: convertData(this.data),
            symbolSize: function (val) {
             let symbolSize= 0
              if(val[2]<=10){
                  symbolSize=val[2]
              }else if(val[2]<=100){
                  symbolSize=20
              }else if(val[2]<=1000){
                  symbolSize=30
              }else{
                  symbolSize=50
              }
              return symbolSize
            },
            label: {
              normal: {
                formatter: '{b}',
                position: 'right',
                show: false
              },
              emphasis: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                color: '#ffeb7b'
              }
            }
          }
        ]
      }
    }
  },
  watch: {
    data(newValue, oldValue) {
      this.option.series[0].data= convertData(newValue)
      this.chart.setOption(this.option);
    },
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      echarts.registerMap('china', china);
      this.chart = echarts.init(this.$el, 'spider')
      this.chart.setOption(this.option)
      this.chart.on('click', param => {
        this.$emit('clickEvent',param.name)
      })
    }
  }
}
</script>
